package com.flying.invest.job;

import java.util.Date;
import java.util.List;

import com.flying.common.util.Codes;
import com.flying.common.util.DateUtils;
import com.flying.common.util.DateUtils.Field;
import com.flying.framework.annotation.Service;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.service.AbstractService;
import com.flying.invest.assets.model.Assets;
import com.flying.invest.assets.service.AssetsPriceService;
import com.flying.invest.assets.service.AssetsService;

@Service(value="AssetsPriceJob", desc="")
public class AssetsPriceJob extends AbstractService {
	private AssetsService assetsService;
	private AssetsPriceService priceService;
	
	public void setAssetsService(AssetsService s) {
		this.assetsService = s;
	}
	public void setAssetsPriceService(AssetsPriceService s) {
		this.priceService = s;
	}
	
	public ActionResult run() {
		var today = new Date();
		var m3day = DateUtils.add(today, Field.MONTH, -3);
		var m6day = DateUtils.add(today, Field.MONTH, -6);
		var m12day = DateUtils.add(today, Field.MONTH, -12);
		List<Assets> assets = this.assetsService.findAll2(0, 100000).getRows();
		assets.stream().filter(x -> x.getPrice() != null).forEach(x -> {
			try {
				var m3 = this.priceService.findTopPriceByAssetsId(x.getAssetsId(), m3day, today);
				var m6 = this.priceService.findTopPriceByAssetsId(x.getAssetsId(), m6day, today);
				var m12 = this.priceService.findTopPriceByAssetsId(x.getAssetsId(), m12day, today);
				this.assetsService.updateHistoryPrice(x.getAssetsId(), 
						m3.getMaxPrice(), m3.getMinPrice(), 
						m6.getMaxPrice(), m6.getMinPrice(), 
						m12.getMaxPrice(), m12.getMinPrice());
			} catch (Exception e) { 
				System.err.println("Assets[" + x.getAssetsId() +"] update top price fail! reason :" + e);
			}
		});
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS);
	}
}
